package edu.pku.sei.modeler.uml.commands.component;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicComponents.Component;
import edu.pku.sei.modeler.model.uml.Ports.Port;
import edu.pku.sei.modeler.model.uml.StructuredClasses.StructuredClass;
import edu.pku.sei.modeler.uml.commands.NestedElementDeleteCommand;

public class PortDeleteCommand extends NestedElementDeleteCommand {

	public PortDeleteCommand(MetaNode node, MetaNode fatherNode,
			boolean isDeletedFromDiagram) {
		super(node, fatherNode, isDeletedFromDiagram);
	}

	@Override
	public void deleteGraphNode() {
		if (fatherNode.getParent() == null) {
			fatherNode.getDiagram().getNodes().remove(this.node);
		} else {
			fatherNode.getParent().getSubNodes().remove(this.node);
			this.node.setParent(null);
		}
	}

	@Override
	public void undoDeleteGraphNode() {
		if (fatherNode.getParent() == null) {
			fatherNode.getDiagram().getNodes().add(this.node);
		} else {
			this.node.setParent(fatherNode.getParent());
			fatherNode.getParent().getSubNodes().add(this.node);

		}
	}

	@Override
	public void deleteModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		if (fatherNode.getModelElement() instanceof StructuredClass) {
			StructuredClass parentStrucuredClass = (StructuredClass)fatherNode.getModelElement();
			if (modelElement instanceof Port) {
				parentStrucuredClass.getOwnedPort().remove(modelElement);
				((Port) modelElement).setStructuredClass(null);
			}
		}
	}

	@Override
	public void undoDeleteModelElement() {
		if (fatherNode.getModelElement() instanceof StructuredClass) {
			MetaModelElement modelElement = node.getModelElement();
			if (modelElement instanceof Port) {
				StructuredClass parentStrucuredClass = (StructuredClass)fatherNode.getModelElement();
				parentStrucuredClass.getOwnedPort().add(modelElement);
				((Port) modelElement).setStructuredClass(parentStrucuredClass);
			}
		}
	}
}
